package com.demo.rabbitmq.demo.pubSub;

import com.demo.rabbitmq.util.RabbitMQUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    static final String FANOUT_EXCHAGE = "fanout_exchange";
    static final String FANOUT_OuRUE1 = "fanout_queue1";
    static final String FANOUT_OuRUE2 = "fanout_queue2";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMQUtil.getConnection();

        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare(FANOUT_EXCHAGE, BuiltinExchangeType.FANOUT);
        //声明创建列队
        channel.queueDeclare(FANOUT_OuRUE1,true,false,false,null);
        channel.queueDeclare(FANOUT_OuRUE2,true,false,false,null);
        //列队绑定到交换机
        channel.queueBind(FANOUT_OuRUE1, FANOUT_EXCHAGE, "");
        channel.queueBind(FANOUT_OuRUE2, FANOUT_EXCHAGE, "");
        for(int i=1;i<=30;i++){
            String message="消息："+i;
            System.out.println(message);
            channel.basicPublish(FANOUT_EXCHAGE,"",null,message.getBytes());
        }
        channel.close();
        connection.close();


    }
}
